<?php

/**
 * 话题推荐管理 -- 推荐提交程序
 * 
 * @copyright caijian
 * @since 2012-03-20
 */




include_once($_SERVER["DOCUMENT_ROOT"] . "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/core/admin.inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/foodv2/inc.php");

$g_cgival = array();
$g_pro = array();
$g_show = array();


function check_cgi_pro()
{
	global $config,$g_cgival, $g_pro, $g_show;
	
	if(! security_check::check_refer())
	{
		throw new Exception("来路不明");
	}
	
	// id
	cgi::both($g_cgival["id"], "id", 0);
	
	// op
	cgi::both($g_cgival["op"], "op", "");
	
	// pos
	cgi::both($g_cgival["event_tj_pos"], "event_tj_pos", 0);
	cgi::both($g_cgival["event_title"], "event_title", '');
	cgi::both($g_cgival["event_content"], "event_content", '');
	cgi::both($g_cgival["event_start_date"], "event_start_date", '');
	cgi::both($g_cgival["event_end_date"], "event_end_date", '');
	cgi::both($g_cgival["event_url"], "event_url", '');
	cgi::both($g_cgival["event_pic"], "event_pic", '');
	cgi::both($g_cgival["nickname"], "nickname", '');
	
	
	if (!string::is_url($g_cgival["event_url"])
	and $g_cgival["op"] != "up"
	and $g_cgival["op"] != "down"
	and $g_cgival["op"] != "off" 
	and  $g_cgival["op"] != "on" 
	and  $g_cgival["op"] != "del" )
	{
		throw new Exception("非法URL地址！");
	}
}

function get_data()
{
	global $config,$g_cgival, $g_pro, $g_show;
	
	$op = $g_cgival["op"];

	// 拿到当前站点的site_id
	$site_id = admin::admin_get_user_site_id();
	$admin_id = admin::admin_get_login_uid();
	$admin_name = admin::admin_get_login_user_name();


	$db = new db_food_event();
	$fuser = new mod_food_user();
	$admin_log = new admin_log();
	

	$event_title = $g_cgival['event_title'];
	$event_content = $g_cgival['event_content'];
	$event_start_date = $g_cgival['event_start_date'];
	$event_end_date = $g_cgival['event_end_date'];
	$event_tj_pos = $g_cgival['event_tj_pos'];
	$event_url = $g_cgival['event_url'];
	$event_pic = $g_cgival['event_pic'];
	$nickname = $g_cgival['nickname'];

	
	if ($op == "add" or $op == "edit")
	{
		// 判断结束日期是否小于当前
		$time_end = strtotime($event_end_date);
		if ($time_end < time())
		{
			// throw new Exception("活动结束时间不能小于今天");
		}
	}
	

	// 排序
	if ($op == 'up' || $op == 'down') {

		$id = intval($g_cgival['id']);
		$data = $db->admin_get_food_event_by_id($id);
		$datanext = $db->admin_get_event_tj_next($data['event_seq'],$op,$data["event_tj_pos"]);
		
		if ( is_array($datanext) && count($datanext) >0) {
			$db->admin_update_food_event_by_id($data['id'],array('event_seq'=>$datanext['event_seq']));
			$db->admin_update_food_event_by_id($datanext['id'],array('event_seq'=>$data['event_seq']));
		}
		
		if ($op == 'up')
		{
			$admin_log->admin_insert_log_info($admin_id, $site_id, 10, "上移 活动id={$id}");
		}
		else
		{
			$admin_log->admin_insert_log_info($admin_id, $site_id, 10, "下移 活动id={$id}");
		}

	// 上下架
	} else if ($op == 'on' || $op == 'off') {

		$id = intval($g_cgival['id']);
		if ($op == 'on' ) $status = 1;
		else $status = 0;

		$data = $db->admin_get_food_event_by_id($id);

		if ($data && $db->admin_update_food_event_by_id($id,array('status'=>$status, 'event_seq'=>0) ) ) {
			return $db->admin_decrease_event_seq($site_id,$event_tj_pos, $data['event_seq']);
		}
		
		if ($op == 'on')
		{
			$admin_log->admin_insert_log_info($admin_id, $site_id, 10, "上架 活动id={$id}");
		}
		else 
		{
			$admin_log->admin_insert_log_info($admin_id, $site_id, 10, "下架 活动id={$id}");
		}

	// 删除
	} else if ( $op == 'del' ) {
		$id = intval($g_cgival['id']);
		$data = $db->admin_del_food_event_by_id($id);
	
		$admin_log->admin_insert_log_info($admin_id, $site_id, 10, "删除 活动id={$id}");
	// 添加
	} else if ( $op == 'add' ) {

		$event_tj_pos = intval($g_cgival['event_tj_pos']);
		if (! $event_tj_pos) $event_tj_pos = 1;

		$db->admin_increase_event_seq($site_id,$event_tj_pos);

		$total_tj_num = $config['food_event_total_tj_num'][$event_tj_pos];
		$arr = array(
			'site_id'	=>	$site_id
			,'event_title'	=>	$event_title
			,'event_content'	=>	$event_content
			,'event_start_date'	=>	$event_start_date
			,'event_end_date'	=>	$event_end_date
			,'event_tj_pos'	=>	$event_tj_pos
			,'event_url'	=>	$event_url
			,'nickname'		=>  $nickname
			,'event_pic'	=>	$event_pic
			,'admin_id'	=>	$admin_id
			,'admin_name'	=>	$admin_name
		);

		// 将超出的活动下架：
		if ($db->admin_insert_food_event($arr)) {
			$db->admin_update_exceed_event_tj_status($site_id,$event_tj_pos,$total_tj_num);
		}

		$admin_log->admin_insert_log_info($admin_id, $site_id, 10, "添加  活动");
	// 编辑
	} else if ( $op == 'edit' ) {

		$event_tj_pos = intval($g_cgival['pos']);
		$id = intval($g_cgival['id']);

		$total_tj_num = $config['food_event_total_tj_num'][$event_tj_pos];
		$arr = array(
			'event_title'	=>	$event_title
			,'event_content'	=>	$event_content
			,'event_start_date'	=>	$event_start_date
			,'event_end_date'	=>	$event_end_date
			,'event_url'	=>	$event_url
			,'event_pic'	=>	$event_pic
			,'nickname'		=>  $nickname
			,'admin_id'	=>	$admin_id
			,'admin_name'	=>	$admin_name
		);
		$admin_log->admin_insert_log_info($admin_id, $site_id, 10, "编辑 活动id={$id}");
		return $db->admin_update_food_event_by_id($id,$arr);

	} else {
		throw new Exception("参数错误！");
	}
}

try
{

	admin_check_login();
// 	admin_check_user_priv("foodv2/main_nav.php");
	
	check_cgi_pro();
	get_data();
	
	$g_show["error"] = 0;
	$g_show["errmsg"] = "成功";	

}
catch(Exception $e)
{
	$g_show["error"] = 1;
	$g_show["errmsg"] = $e->getMessage();
}

show_pro();
exit;


function show_pro()
{
	global $g_show;
	
	echo json_encode($g_show);
}

?>